// pages/addPyq/addPyq.js
var service_common = require("../../service/common.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    image_list:[],
    up_number:'',
    roomID:-1,
    content:"",
    note:"",
    openid:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      up_number: this.randomString(32),
      roomID: options.roomID,
      openid: getApp().globalData.userInfo.openid
    })
  },
  // 生成随机字符串
  randomString: function (len) {
    var len = len || 32;
    var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
    var maxPos = $chars.length;
    var pwd = '';
    for (var i = 0; i < len; i++) {
      pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  // 删除上传的图片
  delImage:function(e){
    this.data.image_list.splice(e.currentTarget.dataset.index, 1)
    this.setData({
      image_list: this.data.image_list
    })
  },
  // 上传图片
  upImage:function(){
    var that = this;
    wx.chooseImage({
      count: 6 - this.data.image_list.length, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success:  (res)=>{
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        for (let i = 0; i < tempFilePaths.length; i++) {
          console.log(i)
          var temp = tempFilePaths[i];
          wx.uploadFile({
            url: service_common.getUrl('user.uploadsss'),
            filePath: temp,
            name: 'file',
            formData: {
              openid: this.data.openid,
              token: wx.getStorageSync('token__'),
              token_key: wx.getStorageSync('token_key'),
              version: getApp().globalData.version,
              real_openid: getApp().globalData.real_openid,
              miniproid: getApp().globalData.miniproid,
              'from':'wechat',
              up_number: this.data.up_number,
            },
            success:  (ret)=>{
              let res = JSON.parse(ret.data)
              if (res.code == 0) {
                this.data.image_list.push(res.data)
                this.setData({
                  image_list: this.data.image_list
                })
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.message,
                })
              }
            },
            fail: function (ret) {
              var res = JSON.parse(ret.data);
              wx.showToast({
                title: res.message,
              })
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 获取文案内容
  setcontent:function(e){
    this.setData({
      content: e.detail.value
    })
  },
  // 获取备注
  setnote: function (e) {
    this.setData({
      note: e.detail.value
    })
  },
  // 发布
  save:function(){
    if (this.data.content.replace('/(^/s *)| (/s*$)/g', "") == ""){
      wx.showToast({
        title: '请输入信息',
        image: '/image/cancel.png',
        duration: 2000
      });
    }else{
      service_common.fetchData(
        service_common.getUrl("user.addMaterial"),
        {
          openid: this.data.openid,
          type: this.data.roomID,
          up_number: this.data.up_number,
          content: this.data.content,
          note: this.data.note
        },
        (res) => {
          if (res.data.code == 0) {
              wx.showToast({
                title: '发布成功，等待审核'
              })
              setTimeout(()=>{
                wx.navigateBack()
              },500)
          }
        },
        "POST"
      )
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})